package com.circle.interests.biz.core.impl;

import cn.hutool.core.util.IdUtil;
import com.circle.common.core.exception.RRException;

import com.circle.interests.biz.core.PrizeWriteOffService;
import com.circle.interests.biz.core.category.honglian.PrizeHongLianHandler;
import com.circle.interests.biz.core.category.phonefee.PrizePhoneFeeHandler;
import com.circle.interests.biz.core.category.wxredpackage.PrizeWxRedPackageHandler;
import com.circle.interests.biz.core.protocol.PrizeWriteOffCondition;
import com.circle.interests.biz.core.protocol.PrizeWriteOffResultDto;
import com.circle.interests.biz.core.protocol.RechargeResult;
import com.circle.interests.biz.enums.PrizeCategoryEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author zhou
 * @date 2021/12/8
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class PrizeWriteOffServiceImpl implements PrizeWriteOffService {

    private final PrizeHongLianHandler hongLianHandler;
    private final PrizePhoneFeeHandler phoneFeeHandler;
    private final PrizeWxRedPackageHandler wxRedPackageHandler;

    @Override
    public PrizeWriteOffResultDto writeOff(PrizeWriteOffCondition condition) {
        PrizeWriteOffResultDto resultDto = new PrizeWriteOffResultDto();
        PrizeCategoryEnum prizeCategory = PrizeCategoryEnum.getByCode(condition.getPrizeCategoryId());
        switch (prizeCategory) {
            case WEIXIN_RED_PACKAGE:
                RechargeResult result = wxRedPackageHandler.recharge(condition.getSceneId(), condition.getOpenId(), condition.getPrizePrice(), condition.getRemark());
                resultDto.setStatus(2);
                resultDto.setMessage("核销成功");
                break;
            case HONG_LIAN:
                hongLianHandler.recharge(IdUtil.getSnowflake().nextIdStr(), condition.getPhoneNumber(), "KMFL127", condition.getRemark());
                resultDto.setStatus(2);
                resultDto.setMessage("核销成功");
                break;
            case SHAN_HU:
                throw new RRException("暂未实现珊瑚会员奖品");
            case MATERIAL:
                resultDto.setStatus(2);
                resultDto.setMessage("核销成功");
                break;
            case PHONE_FEE:
                phoneFeeHandler.recharge(condition.getPhoneNumber(), condition.getPrizePrice());
                resultDto.setStatus(2);
                resultDto.setMessage("核销成功");
                break;
            default:
                throw new RRException("奖品类型错误: " + condition.getPrizeCategoryId());
        }
        return resultDto;
    }
}
